package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Report;
import com.ruoyi.system.domain.TStudentInfo;
import com.ruoyi.system.service.ITStudentInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学员信息Controller
 * 
 * @author ruoyi
 * @date 2021-04-28
 */
@Controller
@RequestMapping("/system/info")
public class TStudentInfoController extends BaseController
{
    private String prefix = "system/info";

    @Autowired
    private ITStudentInfoService tStudentInfoService;

    @RequiresPermissions("system:info:list")
    @GetMapping()
    public String info()
    {
        return prefix + "/info";
    }
    @RequiresPermissions("system:info:list")
    @GetMapping("/normalInfo")
    public String normalInfo()
    {
        return prefix + "/normal";
    }
    /**
     * 查询学员信息列表
     */
    @RequiresPermissions("system:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TStudentInfo tStudentInfo)
    {
        startPage();
     // 获取当前的用户信息
        SysUser user = ShiroUtils.getSysUser();
        if(!user.isAdmin()) {
        	tStudentInfo.setAddUser(user.getUserId()+"");
        	tStudentInfo.setShouhou(user.getUserName());
        }
        List<TStudentInfo> list;
        list = tStudentInfoService.selectTStudentInfoList(tStudentInfo);
        return getDataTable(list);
    }
    /**
     * 查询学员信息列表
     */
    @RequiresPermissions("system:info:list")
    @PostMapping("/listNormal")
    @ResponseBody
    public TableDataInfo listNormal(TStudentInfo tStudentInfo)
    {
        startPage();
        List<TStudentInfo> list;
        		if((null==tStudentInfo.getWxNo()||"".equals(tStudentInfo.getWxNo()))&&(null==tStudentInfo.getQqNo()||"".equals(tStudentInfo.getQqNo()))) {
        			list = new ArrayList<TStudentInfo>();
        			return getDataTable(list);
            	}
        list = tStudentInfoService.selectTStudentInfoList(tStudentInfo);
        return getDataTable(list);
    }
    /**
     * 导出学员信息列表
     */
    @RequiresPermissions("system:info:export")
    @Log(title = "学员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TStudentInfo tStudentInfo)
    {
        List<TStudentInfo> list = tStudentInfoService.selectTStudentInfoList(tStudentInfo);
        ExcelUtil<TStudentInfo> util = new ExcelUtil<TStudentInfo>(TStudentInfo.class);
        return util.exportExcel(list, "学员信息数据");
    }

    /**
     * 新增学员信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学员信息
     */
    @RequiresPermissions("system:info:add")
    @Log(title = "学员信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TStudentInfo tStudentInfo)
    {
    	int rows = 0;
    	try {
    		rows = tStudentInfoService.insertTStudentInfo(tStudentInfo);
    	}catch(Exception ex) {
    		return error("学员在此科目已经报名！");
    	}
        return toAjax(rows);
    }

    /**
     * 修改学员信息
     */
    @GetMapping("/edit/{ID}")
    public String edit(@PathVariable("ID") Long ID, ModelMap mmap)
    {
        TStudentInfo tStudentInfo = tStudentInfoService.selectTStudentInfoById(ID);
        mmap.put("tStudentInfo", tStudentInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存学员信息
     */
    @RequiresPermissions("system:info:edit")
    @Log(title = "学员信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TStudentInfo tStudentInfo)
    {
    	int rows = 0;
    	try {
    		rows = tStudentInfoService.updateTStudentInfo(tStudentInfo);
    	}catch(Exception ex) {
    		return error("学员在此科目已经报名！");
    	}
        return toAjax(rows);
    }

    /**
     * 删除学员信息
     */
    @RequiresPermissions("system:info:remove")
    @Log(title = "学员信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tStudentInfoService.deleteTStudentInfoByIds(ids));
    }
    @RequiresPermissions("system:info:export")
    @PostMapping("/importData")
    @Log(title = "学员信息", businessType = BusinessType.IMPORT)
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
    	ExcelUtil<TStudentInfo> util = new ExcelUtil<TStudentInfo>(TStudentInfo.class);
    	List<TStudentInfo> students = util.importExcel(file.getInputStream());
    	String message = tStudentInfoService.importStudents(students);
    	return AjaxResult.success(message);
    }
    @RequiresPermissions("system:info:export")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TStudentInfo> util = new ExcelUtil<TStudentInfo>(TStudentInfo.class);
        return util.importTemplateExcel("用户数据");
    }
   
    @PostMapping("/selectKemuReport")
    @ResponseBody
    public Report selectKemuReport(){
    	List<Map<String,Object>> datas = tStudentInfoService.selectKemuReport();
    	Report re = new Report();
    	Map<String,Object> data = datas.get(0);
    	re.addLegend(data.get("kemu").toString());
    	re.addData(data.get("一月").toString());
    	re.addData(data.get("二月").toString());
    	re.addData(data.get("三月").toString());
    	re.addData(data.get("四月").toString());
    	re.addData(data.get("五月").toString());
    	re.addData(data.get("六月").toString());
    	re.addData(data.get("七月").toString());
    	re.addData(data.get("八月").toString());
    	re.addData(data.get("九月").toString());
    	re.addData(data.get("十月").toString());
    	re.addData(data.get("十一月").toString());
    	re.addData(data.get("十二月").toString());
    	data = datas.get(1);
    	re.addLegend(data.get("kemu").toString());
    	re.addData2(data.get("一月").toString());
    	re.addData2(data.get("二月").toString());
    	re.addData2(data.get("三月").toString());
    	re.addData2(data.get("四月").toString());
    	re.addData2(data.get("五月").toString());
    	re.addData2(data.get("六月").toString());
    	re.addData2(data.get("七月").toString());
    	re.addData2(data.get("八月").toString());
    	re.addData2(data.get("九月").toString());
    	re.addData2(data.get("十月").toString());
    	re.addData2(data.get("十一月").toString());
    	re.addData2(data.get("十二月").toString());
    	return re;
    	
    }
    @PostMapping("/selectYearReport")
    @ResponseBody
    public Report selectYearReport(){
    	Report report =  doReport(tStudentInfoService.selectYearReport(),null);
    	return doReport(tStudentInfoService.selectYearMoneyReport(),report);
    }
    @PostMapping("/selectHuanbiReport")
    @ResponseBody
    public List<Map<String,Object>> selectHuanbiReport(){
    	return tStudentInfoService.selectKemuHuanbi();
    }
    @PostMapping("/selectMoshiReport")
    @ResponseBody
    public Report selectMoshiReport(){
    	return doReport(tStudentInfoService.selectMoshiReport(),null);
    }
    private Report doReport(List<Map<String,Object>> datas,Report report) {
    	if(report==null) {
	    	report = new Report();
	    	for(Map<String,Object> data : datas) {
	    		report.addLegend(data.get("NAME").toString());
	    		report.addData(data.get("VALUE"));
	    	}
    	}else {
    		for(Map<String,Object> data : datas) {
	    		report.addData2(data.get("VALUE"));
	    	}
    	}
    	
    	return report;
    }
}
